//
//  GMTabBarControllerConfig.m
//  gentleman
//
//  Created by 范月成 on 2017/12/5.
//  Copyright © 2017年 fancy. All rights reserved.
//

#import "GMTabBarControllerConfig.h"
#import "GMNavigationController.h"
#import "GMArtListViewController.h"
#import "GMSettingViewController.h"
#import "UIColor+GMAdd.h"

@interface GMTabBarControllerConfig () <UITabBarControllerDelegate>

@property (nonatomic, readwrite, strong) CYLTabBarController *tabBarController;

@end

@implementation GMTabBarControllerConfig

- (CYLTabBarController *)tabBarController {
    if (!_tabBarController) {
        UIEdgeInsets imageInsets = UIEdgeInsetsZero;
        UIOffset titlePositionAdjustment = UIOffsetMake(0, -4);
        
        _tabBarController = [CYLTabBarController tabBarControllerWithViewControllers:self.viewControllers tabBarItemsAttributes:self.tabBarItemsAttributesForController imageInsets:imageInsets titlePositionAdjustment:titlePositionAdjustment];
        
        [self customizeTabBarAppearance:_tabBarController];
    }
    return _tabBarController;
}

- (NSArray *)viewControllers {
    GMNavigationController *home = [[GMNavigationController alloc] initWithRootViewController:[GMArtListViewController new]];
    
    GMNavigationController *search = [[GMNavigationController alloc] initWithRootViewController:[[GMArtListViewController alloc] initWithType:GMArtListTypeSearch]];
    
    GMNavigationController *setting = [[GMNavigationController alloc] initWithRootViewController:[GMSettingViewController new]];
    
    NSArray *viewControllers = @[home, search, setting];
    
    return viewControllers;
}


- (NSArray *)tabBarItemsAttributesForController {
    NSDictionary *home = @{CYLTabBarItemTitle : @"首页",
                           CYLTabBarItemImage : @"tab_home",
                           CYLTabBarItemSelectedImage : @"tab_homeHL"};
    
    NSDictionary *search = @{CYLTabBarItemTitle : @"搜索",
                             CYLTabBarItemImage : @"tab_search",
                             CYLTabBarItemSelectedImage : @"tab_searchHL"};
    
    NSDictionary *setting = @{CYLTabBarItemTitle : @"设置",
                               CYLTabBarItemImage : @"tab_setting",
                               CYLTabBarItemSelectedImage : @"tab_settingHL"};
    
    NSArray *tabBarItemsAttributes = @[home, search, setting];
                                    
    return tabBarItemsAttributes;
}

/**
 *  更多TabBar自定义设置：比如：tabBarItem 的选中和不选中文字和背景图片属性、tabbar 背景图片属性
 */
- (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarController {
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor themeGray];
    
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor themePink];
    
    // 设置文字属性
    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];

    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
}


@end

